<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>骡窝窝系统管理平台</title>
    <#include "../common/header.ftl"/>
    <link href="/js/plugins/treeview/bootstrap-treeview.min.css" type="text/css" rel="stylesheet">
    <script src="/js/plugins/treeview/bootstrap-treeview.min.js"></script>
    <script src="/js/plugins/messager/jquery.bootstrap.min.js"></script>

    <script type="application/javascript">

            $(function () {

                $('#editForm').ajaxForm(function(data){
                    if(data.success){
                        location.reload();
                    }else{
                        $.messager.alert("温馨提示",data.msg);
                    }
                });


                $('.submitBtn').click(function(){
                    $('#editForm').submit();
                });

                $.get('/region/selectByParentId.do',{type:"tree"},function (data) {
                    $('#treeview').treeview({
                        data:[{text:'全部地区',nodes:data}],
                        showTags:true,
                        lazyLoad:function (node) {
                        $.get('/region/selectByParentId.do',{parentId:node.id,type:"tree"},function (list) {
                        $('#treeview').treeview('addNode',[ list ,node ]);
                            });

                        },
                        //选择节点事件
                        onNodeSelected:function (event,data) {
                            $.get('/region/selectByParentId.do',{parentId:data.id},function (list) {
                                var temp="";
                                $.each(list,function (index,ele) {
                                    temp+="<tr><td>" +(index+1)+ "</td><td>"+ ele.name+"</td>"
                                    +"<td>" + "<a data-json='"+ ele.json + "'" + "class=\"btn btn-info btn-xs inputBtn\" href=\"javascript:\">\n" +
                                            " <span class=\"glyphicon glyphicon-pencil \"></span>编辑\n" +
                                            " </a>\n" +
                                            "</td>"
                                            +
                                    "</tr>"
                                });
                                $("tbody").html(temp);
                                inputRegion();
                            });
                            
                        }
                    });
                });
            })


    </script>



</head>

<body>

<div class="container">
<#include "../common/top.ftl"/>
    <div class="row">
        <div class="col-sm-3">
		<#assign currentMenu = "region" />
				<#include "../common/menu.ftl" />
        </div>
        <div class="col-sm-9">
            <div class="page-header">
                <h3>地区管理</h3>
            </div>
            <div class="row">
                <div class="form-group" style="margin-left: 20px">
                    <a href="javascript:void(-1);" class="btn btn-success inputBtn" id="addRegionBtn">添加地区</a>
                </div>
            </div>
            <br/>
            <div class="row">
                <div class="col-sm-4">
                    <div id="treeview" >

                    </div>
                </div>
                <div class="col-sm-8">
                    <table id="regionTable" class="table">
                        <thead>
                        <tr>
                            <th>序号</th>
                            <th>名称</th>
                        </tr>
                        </thead>
                        <tbody>

                        </tbody>
                    </table>


                    <div style="text-align: center;">
                        <ul id="pagination" class="pagination"></ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Modal -->
<div class="modal fade" id="inputModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">地区管理</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/region/saveOrUpdate.do" method="post" id="editForm">
                    <input type="hidden"  name="id">
                    <div class="form-group" >
                        <label for="name" class="col-sm-4 control-label">名称：</label>
                        <div class="col-lg-7">
                            <input type="text" class="form-control" id="name" name="name">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="sn" class="col-sm-4 control-label">上级地区：</label>
                        <div class="col-lg-7">
                            <input type="text" class="form-control" id="parentName" name="parent.name" readonly>
                            <input type="hidden" class="form-control" id="parent.id" name="parent.id" >
                        </div>
                    </div>
                    <div class="form-group" >
                        <label for="name" class="col-sm-4 control-label">状态：</label>
                        <div class="col-lg-7">
                            <select name="state" class="form-control" >
                                <option value="0">普通</option>
                                <option value="1">推荐</option>

                            </select>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary submitBtn">保存</button>
            </div>
        </div>
    </div>
</div>
<script>
function inputRegion(){
    $(".inputBtn").click(function(){
        $('#editForm input').val("");
        var obj=$(this).data("json");
        console.log(obj);
        if(obj){
            $('#editForm input[name="id"]').val(obj.id);
            $('#editForm input[name="name"]').val(obj.name);
            $('#editForm input[name="parent.id"]').val(obj.parentId);
            $('#editForm select[name="state"]').val(obj.state);
            $('#editForm input[name="parent.name"]').val(obj.parentName);
        }else{
            var ad = $('#treeview').treeview('getSelected');
            console.log(ad);
             $('#editForm input[name="parent.id"]').val(ad[0].id);
              $('#editForm input[name="parent.name"]').val(ad[0].text);
        }
        $('#inputModal').modal('show')
    });

}
inputRegion();
</script>
</body>
</html>